package com.shudes.container;

import java.util.*;

public class ContainerMap<P,C> implements Iterable<Container<P,C>>{
	protected HashMap<P,Container<P,C>> containers;
	
	public ContainerMap() {
		this.containers = new HashMap<P,Container<P,C>>();		
	}
	
	public void addChild(P parent, C child) {
		Container<P,C> c; 
		c = getContainer(parent); 
		if(c==null) 
			c = newContainer(parent); 
		c.addChild(child); 
		containers.put(parent,c); 			
	}
	
	public void addContainer(Container<P,C> container) {
		containers.put(container.getParent(), container); 
	}
	
	public Container<P,C> getContainer(P parent) { 
		return containers.get(parent);
	}
	
	public Container<P,C> newContainer(P parent) {
		return new Container<P,C>(parent); 
	}
	
	public ContainerMap<P,C> newContainerMap() {
		return new ContainerMap<P,C>();
	}
	
	public Iterator<Container<P,C>> iterator() {
		return containers.values().iterator();
	}
}
